/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFramePrincipal.java
 *
 * Created on 05/04/2010, 00:18:20
 */

package GUI;

import Client.MyMessageListener;
import Client.MessageManager;
import Client.SocketMessageManager;
import java.awt.BorderLayout;

/**
 *
 * @author Gabriel
 */
public class JFramePrincipal extends javax.swing.JFrame {

    panelLogon logon;
    panelConectando conectando;
    panelChat1 chat;
    private MessageManager messagemanager;
    private MyMessageListener messagelistener;

    /** Creates new form JFramePrincipal */
    public JFramePrincipal(MessageManager manager) {
        super("Simple Talk v0.1");
        initComponents();
        setResizable(false);
        messagemanager = manager;
        messagelistener = new MyMessageListener(chat);
        logon = new panelLogon(messagemanager, messagelistener, this);
        conectando = new panelConectando();
        chat = new panelChat1();

        

      //  logon.setBorder(javax.swing.BorderFactory.createTitledBorder("Pagamento"));
            logon.setSize(jPanel1.getSize());
            jPanel1.add(logon, new BorderLayout());
            jPanel1.paintAll(jPanel1.getGraphics());
            logon.setVisible(true);
            jMenuBar1.setVisible(false);

            repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        Desconectar = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(204, 255, 204));
        jPanel1.setPreferredSize(new java.awt.Dimension(610, 522));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 610, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 522, Short.MAX_VALUE)
        );

        jMenuBar1.setBackground(new java.awt.Color(0, 204, 204));

        jMenu1.setBackground(new java.awt.Color(0, 204, 204));
        jMenu1.setText("Arquivo");

        jMenuItem1.setBackground(new java.awt.Color(0, 204, 204));
        jMenuItem1.setText("Enviar arquivo");
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu3.setBackground(new java.awt.Color(0, 204, 204));
        jMenu3.setText("Opções");

        jMenuItem3.setBackground(new java.awt.Color(0, 204, 204));
        jMenuItem3.setText("Alterar apelido");
        jMenu3.add(jMenuItem3);

        jMenuBar1.add(jMenu3);

        jMenu2.setBackground(new java.awt.Color(0, 204, 204));
        jMenu2.setText("Sair");
        jMenu2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu2ActionPerformed(evt);
            }
        });

        Desconectar.setBackground(new java.awt.Color(0, 204, 204));
        Desconectar.setText("Desconectar");
        Desconectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DesconectarActionPerformed(evt);
            }
        });
        jMenu2.add(Desconectar);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu2ActionPerformed
        

    }//GEN-LAST:event_jMenu2ActionPerformed

    private void DesconectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DesconectarActionPerformed
        messagemanager.disconnect( messagelistener );
        offVisible();
        logon.setSize(jPanel1.getSize());
            jPanel1.add(logon, new BorderLayout());
            jPanel1.paintAll(jPanel1.getGraphics());
            logon.setVisible(true);
            jMenuBar1.setVisible(false);

            repaint();
    }//GEN-LAST:event_DesconectarActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
       messagemanager.disconnect( messagelistener );
       System.exit(0);
    }//GEN-LAST:event_formWindowClosing

    /**
    * @param args the command line arguments
    */

   
   // Displays new message by appending message to JTextArea.  Should
   // be executed only in Event thread; modifies live Swing component
   

    public static void main(final String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                MessageManager messageManager; // declare MessageManager

                if ( args.length == 0 )
                // connect to localhost
                 messageManager = new SocketMessageManager( "localhost" );
             else
                // connect using command-line arg
                messageManager = new SocketMessageManager( args[ 0 ] );

                new JFramePrincipal(messageManager).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Desconectar;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    public void offVisible(){
        if(logon.isVisible()){
            logon.setVisible(false);
        }
         if(conectando.isVisible()){
            conectando.setVisible(false);
        }
         if(chat.isVisible()){
            chat.setVisible(false);
        }
    }
      public void setarChat(){

     offVisible();
            //teste2.setBorder(javax.swing.BorderFactory.createTitledBorder("Cadastro de clientes"));
            chat.setSize(jPanel1.getSize());
            jPanel1.add(chat, new BorderLayout());
            jPanel1.paintAll(jPanel1.getGraphics());
            chat.setVisible(true);
            jMenuBar1.setVisible(true);
            repaint();
    }
}
